package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class gestorSQL {

	public static final String IDIOMA = "idioma";
	public static final String TEMA = "tema";

	private Connection conn;

	public void closeBDConnection() {
		if (conn != null) {
			try {
				if (!conn.isClosed()) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void setConfiguracion(String campo, String valor) {
		try {
			Class.forName("org.sqlite.JDBC");
			Connection conn = DriverManager
					.getConnection("jdbc:sqlite:config.db");
			Statement stat = conn.createStatement();
			// stat.executeUpdate("drop table if exists unidireccional;");
			// //////////////////////////////////////////////String,String
			stat.executeUpdate("create table if not exists config (campo, valor);");
			ResultSet rs = stat
					.executeQuery("select * from config where campo='" + campo
							+ "';");
			PreparedStatement prep;
			if (rs.next()) {
				// System.out.println("rs != null");
				prep = conn
						.prepareStatement("update config set valor=? where campo=?;");
				prep.setString(1, valor);
				prep.setString(2, campo);
			} else {
				// System.out.println("rs == null");
				prep = conn
						.prepareStatement("insert into config values (?, ?);");
				prep.setString(1, campo);
				prep.setString(2, valor);
			}
			prep.addBatch();

			conn.setAutoCommit(false);
			prep.executeBatch();
			conn.setAutoCommit(true);

			rs.close();
			conn.close();
		} catch (SQLException ex) {
			System.out.println(ex.getMessage());
		} catch (ClassNotFoundException ex) {
			System.out.println(ex.getMessage());
		}
	}

	/**
	 * 
	 * @param campo
	 * @return
	 */
	public String getConfiguracion(String campo) {
		String resultado = null;
		try {
			Class.forName("org.sqlite.JDBC");
			Connection conn = DriverManager
					.getConnection("jdbc:sqlite:config.db");
			Statement stat = conn.createStatement();
			// stat.executeUpdate("drop table if exists unidireccional;");
			// //////////////////////////////////////////////String,String
			ResultSet rs = stat
					.executeQuery("select * from config where campo='" + campo
							+ "';");
			// conn.close();
			if (rs != null) {
				try {
					if (rs.next()) {
						resultado = rs.getString("valor");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			rs.close();
			conn.close();
		} catch (SQLException ex) {
			System.out.println(ex.getMessage());
		} catch (ClassNotFoundException ex) {
			System.out.println(ex.getMessage());
		}
		return resultado;
	}

	public ResultSet getConfiguracionCompleta() {
		try {
			Class.forName("org.sqlite.JDBC");
			conn = DriverManager
					.getConnection("jdbc:sqlite:config.db");
			Statement stat = conn.createStatement();
			// stat.executeUpdate("drop table if exists unidireccional;");
			// //////////////////////////////////////////////String,String
			ResultSet rs = stat.executeQuery("select * from config;");
			// conn.close();
			return (rs);
		} catch (SQLException ex) {
			System.out.println(ex.getMessage());
		} catch (ClassNotFoundException ex) {
			System.out.println(ex.getMessage());
		}
		return null;
	}

}
